package com.example.demo.controller;

import com.example.demo.pojo.dto.PersonDTO;
import com.example.demo.pojo.entity.Person;
import com.example.demo.service.MyHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Wei
 * @date 2024/05/19
 */

@RestController
@RequestMapping("/admin")
public class PersonController {


    @Autowired
    private MyHash myHash;

    /**
     * 查询联系人
     *
     * @param name 名称
     * @return {@link List}<{@link Person}>
     */
    @GetMapping
    public List<Person> getAll(@RequestParam(required = false) String name) {
        System.out.println("name = " + name);
        if (name == null) {
            return myHash.getAllContacts();
        }

        ArrayList<Person> res = new ArrayList<>();
        res.add(myHash.search(name));
        System.out.println(res.toString());
        return res;
    }


    @PutMapping
    public Integer savePerson(@RequestBody PersonDTO dto) {
        System.out.println(dto);
        if(dto.getName()==null||dto.getPhone()==null||dto.getAddress()==null)
            return null;
        return myHash.insert(dto.getName(), dto.getPhone(), dto.getAddress());
    }

    @DeleteMapping
    public Integer delete(@RequestParam(required = false) String name) {
        return myHash.delete(name);
    }

}
